package mi.payment.ui.base;

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import lainey.ui.utils.LogUtil;
import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.db.dao.PayDetail;
import mi.payment.db.dao.UserInfo;
import mi.payment.transaction.AutoFinishActivity;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.NumberUtil;
import sunmi.payment.L3.L3BroadcastHelper;
import sunmi.payment.L3.L3ErrorCode;

/**
 * 输入主管密码基类
 */
public class V2BaseInputAdminPwdActivity extends AutoFinishActivity {

    public TextView mTvTitle;

    private View mOKView;
    private View mPasswordHintView;
    private PasswordKeyboard mPasswordKeyboard;
    private PasswordInputView mPasswordInputView;

    private View mRevokeView;
    private TextView mTvType;
    private TextView mTvCardNo;
    private TextView mTvAmount;
    private TextView mTvTime;
    private TextView mTvVoucherNo;
    private TextView mTvReferenceNo;

    private ToneGenerator mToneGenerator;

    public void onClickOK() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_activity_input_admin_password);
        initView();
        initToneGenerator();
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        mTvTitle = titleView.getCenterTextView();
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );

        mRevokeView = findViewById(R.id.view_revoke);
        mTvType = findViewById(R.id.tv_type);
        mTvCardNo = findViewById(R.id.tv_card_num);
        mTvAmount = findViewById(R.id.tv_money);
        mTvTime = findViewById(R.id.tv_time);
        mTvVoucherNo = findViewById(R.id.tv_voucher);
        mTvReferenceNo = findViewById(R.id.tv_reference);

        mOKView = findViewById(R.id.mb_ok);
        mPasswordHintView = findViewById(R.id.tv_password_hint);
        mPasswordKeyboard = findViewById(R.id.passwordKeyboard);
        mPasswordInputView = findViewById(R.id.passwordInputView);

        mOKView.setOnClickListener(this);
        mPasswordKeyboard.setKeyClickCallback(
                new PasswordKeyboard.KeyClickCallback() {

                    @Override
                    public void onNumClick(int keyNum) {
                        sound();
                        mPasswordInputView.addText(keyNum + "");
                    }

                    @Override
                    public void onDelClick() {
                        mPasswordInputView.delLast();
                    }

                    @Override
                    public void onEnterClick() {
                        boolean isPasswordRight = isPasswordRight();
                        if (isPasswordRight) {
                            onClickOK();
                        } else {
                            mPasswordInputView.clearText();
                            showToast(R.string.error_password);
                        }
                    }

                }
        );
    }

    @Override
    public void onBackPressed() {
        L3BroadcastHelper.sendFailedBroadcast(mContext, L3ErrorCode.ERROR_TRADE_CANCEL);
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.mb_ok:
                onClickOK();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mToneGenerator != null) {
            mToneGenerator.release();
        }
    }

    public void fullData(PayDetail payDetail) {
        if (payDetail == null) {
            mRevokeView.setVisibility(View.GONE);
        } else {
            mRevokeView.setVisibility(View.VISIBLE);

            String temp = getString(R.string.revoke_type);
            if (payDetail.transactionType == 1) {
                temp += getString(R.string.consume_revoke);
            } else if (payDetail.transactionType == 4) {
                temp += getString(R.string.pre_auth_revoke);
            } else if (payDetail.transactionType == 6) {
                temp += getString(R.string.pre_auth_complete_revoke);
            }
            mTvType.setText(temp);

            if (payDetail.CardNo != null && payDetail.CardNo.length() > 0) {
                mTvCardNo.setVisibility(View.VISIBLE);
                temp = getString(R.string.revoke_card) + NumberUtil.intercept(payDetail.CardNo);
                mTvCardNo.setText(temp);
            } else {
                mTvCardNo.setVisibility(View.GONE);
            }

            temp = getString(R.string.revoke_money) + MoneyUtils.longCent2DoubleMoneyStr(payDetail.amount);
            mTvAmount.setText(temp);

            temp = payDetail.TradeTime.substring(0, 2) + ":" + payDetail.TradeTime.substring(2, 4) + ":" + payDetail.TradeTime.substring(4, 6);
            temp = getString(R.string.revoke_time) + temp;
            mTvTime.setText(temp);

            temp = getString(R.string.revoke_voucher) + payDetail.voucherNo;
            mTvVoucherNo.setText(temp);

            temp = getString(R.string.revoke_reference) + payDetail.referNo;
            mTvReferenceNo.setText(temp);
        }
    }

    public void showPasswordView(boolean isShow) {
        if (isShow) {
            mOKView.setVisibility(View.GONE);
            mPasswordHintView.setVisibility(View.VISIBLE);
            mPasswordKeyboard.setVisibility(View.VISIBLE);
            mPasswordInputView.setVisibility(View.VISIBLE);
        } else {
            mOKView.setVisibility(View.VISIBLE);
            mPasswordHintView.setVisibility(View.GONE);
            mPasswordKeyboard.setVisibility(View.GONE);
            mPasswordInputView.setVisibility(View.GONE);
        }
    }

    private void initToneGenerator() {
        try {
            mToneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, ToneGenerator.MAX_VOLUME);
        } catch (Exception e) {
            LogUtil.e(Configs.TAG, "打开ToneGenerator失败");
            e.printStackTrace();
        }
    }

    private void sound() {
        if (mToneGenerator != null) {
            mToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);
        }
    }

    /**
     * 判断主管密码是否正确
     */
    private boolean isPasswordRight() {
        String password = mPasswordInputView.getPassword();
        UserInfo login = mUserInfoModel.login("00", password);
        return login != null && login.getUPwd().equals(password);
    }


}
